<template>
    <div>
        <van-address-edit
            :area-list="areaList"
            :address-info="addressInfo"
            show-set-default
            show-delete
            @save="onSave"
            :is-saving="loading"
            @delete="onDelete"
        />
    </div>
</template>

<script>
import areaList from './../../lib/area';
export default {
    name: 'editArea',
    data() {
        return {
            areaList,

            loading: false,

            addressInfo: {
                addressDetail:"",
                areaCode:"",
                city:"",
                country:"",
                county:"",
                isDefault:false,
                name:"",
                postalCode:"",
                province:"",
                tel:"",
            }
        }
    },

    methods: {
        onSave(data) {
            this.loading = true;
            let getCode = (arr, name) => {
                for (let i in this.areaList[arr]) {
                    if (this.areaList[arr][i] === name) {
                        return i;
                    }
                }
                return '';
            }
            let params = {
                name: data.name,
                tel: data.tel,
                proId: getCode('province_list', data.province), //省份ID
                cityId: getCode('city_list', data.city), //城市ID
                districtId: getCode('county_list', data.county), //区县ID
                proName: data.province,
                cityName: data.city,
                districtName: data.county,
                address: data.addressDetail,
                defaultTag: Number(data.isDefault),
                addrId: this.$route.query.addrid
            }

            this.$http.post('/shop/userDeliAddr/front/modifyDeliAddr', params).then(res => {
                this.loading = false;
                if (res.status === 0) {
                    this.$toast.success('修改成功');
                    setTimeout(() => {
                        this.$router.back();
                    }, 700)
                } else {
                    this.$toast.fail(res.message);
                }
            })
        },
        onDelete() {
            this.$http.post('/shop/userDeliAddr/front/delDeliAddr', {
                addrId: this.$route.query.addrid
            }).then(res => {
                if (res.status === 0) {
                    this.$toast.success('删除成功');
                    setTimeout(() => {
                        this.$router.back();
                    }, 700)
                } else {
                    this.$toast.fail(res.message);
                }
            })
        },
        
        getDetail( ) {
            this.$http.post('/shop/userDeliAddr/front/getDeliAddr', {
                addrId: this.$route.query.addrid
            }).then(res => {
                if (res.status === 0) {
                    let info = res.data;
                    /**
                     *  addrId: 10
                        address: "123"
                        cityId: "110100"
                        cityName: "北京市"
                        createTime: "2019-03-12 00:01:14"
                        defaultTag: 0
                        deliName: "1"
                        districtId: "110101"
                        districtName: "东城区"
                        provinceId: "110000"
                        provinceName: "北京市"
                        tel: "13548948212"
                        userId: 1790
                        userName: "8613548948212"
                     */

                    this.addressInfo = {
                        addressDetail:info.address,
                        areaCode: info.districtId,
                        city: info.cityId,
                        county: info.districtName,
                        isDefault: Boolean(info.defaultTag),
                        name: info.deliName,
                        province: info.provinceId,
                        tel: info.tel,
                    }
                } else {
                    this.$toast.fail(res.message);
                }
            })
        }
    },

    created() {
        this.getDetail();
    }
}
</script>

<style lang="scss" scoped>
@import './../../assets/default.scss';
/deep/ .van-button--danger {
    background-color: $defaultColor;
}
</style>
